﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Entity;
using Domain.Service;
using Service.Repository.Sys;
using CommonCode;

namespace Service.App
{
    public class LoginApp
    {
        private IUserRepository<Sys_User> user;
        public LoginApp(IUserRepository<Sys_User> _user)
        {
            user = _user;
        }

        public Sys_User CheckLogin(string usercode, string password)
        {
            Sys_User model = user.FindEntity(t => t.UserCode == usercode);
            if (model != null)
            {
                if (model.EnableLogin == true)
                {
                    if(DESEncrypt.Encrypt(password) == model.PassWord)
                    {
                        model.LogOnCount = model.LogOnCount + 1;
                        model.LastLogOnTime = DateTime.Now;
                        model.LastLogOnIpAdress = Net.Parm_IpAdress;
                        model.LastLogOnIpAdressName = Net.GetLocation(model.LastLogOnIpAdress);
                        user.Update(model);
                        return model;
                    }
                    else
                    {
                        throw new Exception("密码不正确，请重新输入"); 
                    }
                }
                else
                {
                    throw new Exception("账户被系统锁定,请联系管理员");
                }
            }
            else
            {
                throw new Exception("账户不存在，请重新输入");
            }
        }
    }
}
